<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class MainController extends AdminbaseController {
	
    public function index(){
       //时间搜索限制
        if(isset($_GET['type']) && $_GET['type'] == 1){
            $time_arr = getTimestampArr($_GET['value']);
            $recharge_where['time'] = $charge_where['end_time']=array("BETWEEN","$time_arr[0],$time_arr[1]");
        }
        
        if(isset($_POST['begin_time'])){
            $time_arr[0] = $_POST['begin_time'] ? strtotime($_POST['begin_time']) : 0;
            $time_arr[1]   = $_POST['end_time']   ? strtotime($_POST['end_time'])   : time();
            $recharge_where['time'] = $charge_where['end_time']=array("BETWEEN","$time_arr[0],$time_arr[1]");
            $this->assign('begin_time',$_POST['begin_time']);
            $this->assign('end_time',$_POST['end_time']);
        }
        
        $Recharge_log = M('recharge_log');
        $Charge_log   = M('Charge_log');
        $home['recharge_user_count'] =   count($Recharge_log->group('user_id')->where($recharge_where)->select());//充值客户数量
        $home['recharge_money_sum']      = $Recharge_log->where($recharge_where)->getField("sum(amount)");//充值金额数量
        $home['charge_money_sum']        = $Charge_log->where($charge_where)->getField('sum(amount)');
       
        //客户充值top20
        $recharge_customer_top20 = $Recharge_log->where($recharge_where)->field('user_id,max(time) as recharge_time_max,max(amount) as recharge_money_max,sum(amount) as recharge_money_sum')->order("recharge_money_max desc")->group('user_id')->limit(0,20)->select();
        
        $User = M('user');
        foreach ($recharge_customer_top20 as $key => $val) {
            $charge_where['user_id'] = $val['user_id'];
            $recharge_customer_top20[$key]['user_name']   = $User->where('id ='.$val['user_id'])->getField('mobile');
            $recharge_customer_top20[$key]['charge_info'] = $Charge_log->where($charge_where)->field("sum(amount) as charge_money_sum,max(end_time) as charge_time_max")->find();    
             
        }

        //合计
        foreach ($recharge_customer_top20 as $key => $val) {
            $total_info['recharge_money_sum']+=$val['recharge_money_sum'];
            $total_info['charge_money_sum']+=$val['charge_info']['charge_money_sum'];
            $total_info['customer_balance']+=($val['recharge_money_sum']-$val['charge_info']['charge_money_sum']);
        }

        $this->assign('home',$home);
        $this->assign('recharge_customer_top20',$recharge_customer_top20);
        $this->assign('total_info',$total_info);
    	$this->display();
    }
}